﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using Caliburn.Micro;
using Microsoft.Phone.Controls;
using WPCloudPT.ViewModels;

namespace WPCloudPT
{
    /// <summary>
    /// bootstrapper da aplicacao sao definidos aqui as viewmodels(sao ligados as views internamente pelo caliburn.micro)
    /// e definidas convencoes
    /// </summary>
    
    public class AppBootstrapper : PhoneBootstrapper
    {
        #region ClassVars
        /// <summary>
        /// variaveis da classe
        /// </summary>
        private PhoneContainer _container;
        #endregion

        #region Metodos
        /// <summary>
        /// metodos da framework
        /// </summary>
        protected override void Configure()
        {
            _container= new PhoneContainer(RootFrame);
            _container.RegisterPhoneServices();
            _container.PerRequest<MainPageViewModel>();
            _container.PerRequest<PNavPageViewModel>();
            _container.PerRequest<MessageViewModel>();
            _container.PerRequest<InfoUserViewModel>();
            _container.PerRequest<ConteudoViewModel>();
            //_container.RegisterSingleton(typeof(MainPageViewModel), "MainPageViewModel",typeof(MainPageViewModel));
            //_container.RegisterSingleton(typeof(PNavPageViewModel), "PNavPageViewModel",typeof(PNavPageViewModel));
            //_container.RegisterInstance(typeof(INavigationService),null,new FrameAdapter(RootFrame));
            //_container.RegisterInstance(typeof(IPhoneService), null, new PhoneApplicationServiceAdapter(PhoneService));
            //_container.Activator.InstallLauncher<WebBrowserTask>(); 
            AddCustomConventions();

        }

        private static void AddCustomConventions()
        {
            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };
        }


        protected override object GetInstance(Type service, string key)
        {
            return _container.GetInstance(service, key);
        }
        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return _container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);
        }
        #endregion
    }
}
